// MBProgressHUDManager.m
#import "MBProgressHUDManager.h"

@implementation MBProgressHUDManager

+ (UIView *)defaultView {
    return [UIApplication sharedApplication].keyWindow ?: [UIApplication sharedApplication].delegate.window;
}

+ (BOOL)hasHUDInView:(UIView *)view {
    view = view ?: [self defaultView];
    if (!view) return NO;
    
    // 遍历视图的子视图，查找MBProgressHUD
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[MBProgressHUD class]]) {
            return YES;
        }
    }
    return NO;
}

+ (MBProgressHUD *)showHUD {
    return [self showHUDAddedTo:nil animated:YES];
}

+ (MBProgressHUD *)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
    view = view ?: [self defaultView];
    if (!view) return nil;
    
    // 检查是否已存在HUD
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud) {
        // 如果已存在，直接返回现有HUD
        return hud;
    }
    
    // 不存在则创建新的HUD
    hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
    hud.removeFromSuperViewOnHide = YES;
    hud.graceTime = 0.5;
    return hud;
}

+ (MBProgressHUD *)showHUDInView:(UIView *)view withMessage:(NSString *)message afterDelay:(NSTimeInterval)delay completionBlock:(void (^)(void))completionBlock {
    view = view ?: [self defaultView];
    if (!view) return nil;
    
    // 检查是否已存在HUD
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud) {
        // 如果已存在，更新现有HUD
        hud.label.text = message;
        hud.mode = MBProgressHUDModeIndeterminate;
        [hud hideAnimated:YES afterDelay:delay];
        hud.completionBlock = completionBlock;
        return hud;
    }
    
    // 不存在则创建新的HUD
    hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.margin = 10.f;
    hud.mode = MBProgressHUDModeIndeterminate;
    [hud hideAnimated:YES afterDelay:delay];
    hud.completionBlock = completionBlock;
    return hud;
}

+ (MBProgressHUD *)showTextHUDInView:(UIView *)view withMessage:(NSString *)message afterDelay:(NSTimeInterval)delay {
    view = view ?: [self defaultView];
    if (!view) return nil;
    
    // 检查是否已存在HUD
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud) {
        // 如果已存在，更新现有HUD
        hud.label.text = message;
        hud.mode = MBProgressHUDModeText;
        [hud hideAnimated:YES afterDelay:delay];
        return hud;
    }
    
    // 不存在则创建新的HUD
    hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.margin = 10.f;
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}

+ (MBProgressHUD *)showTextHUDInView:(UIView *)view withMessage:(NSString *)message yOffset:(CGFloat)yOffset afterDelay:(NSTimeInterval)delay {
    view = view ?: [self defaultView];
    if (!view) return nil;
    
    // 检查是否已存在HUD
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud) {
        // 如果已存在，更新现有HUD
        hud.label.text = message;
        hud.mode = MBProgressHUDModeText;
        CGPoint point = hud.offset;
        point.y = yOffset;
        hud.offset = point;
        [hud hideAnimated:YES afterDelay:delay];
        return hud;
    }
    
    // 不存在则创建新的HUD
    hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.margin = 10.f;
    CGPoint point = hud.offset;
    point.y = yOffset;
    hud.offset = point;
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}

+ (MBProgressHUD *)showDetailTextHUDInView:(UIView *)view withMessage:(NSString *)message afterDelay:(NSTimeInterval)delay {
    view = view ?: [self defaultView];
    if (!view) return nil;
    
    // 检查是否已存在HUD
    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
    if (hud) {
        // 如果已存在，更新现有HUD
        hud.detailsLabel.text = message;
        hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        hud.mode = MBProgressHUDModeText;
        [hud hideAnimated:YES afterDelay:delay];
        return hud;
    }
    
    // 不存在则创建新的HUD
    hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    [hud hideAnimated:YES afterDelay:delay];
    return hud;
}

+ (void)hideHUD {
    [self hideHUDForView:nil animated:YES];
}

+ (void)hideHUDForView:(UIView *)view animated:(BOOL)animated {
    view = view ?: [self defaultView];
    if (!view) return;
    
    [MBProgressHUD hideHUDForView:view animated:animated];
}

@end
